import { defineStore } from 'pinia'
import type { StoreDefinition } from 'pinia'
import { IChatStore } from './type'

import { chatLogin, chatRegister } from '@/api/chat'


export const useChatStore: StoreDefinition<string, IChatStore> = defineStore('chat', {
  state: () => {
    return {
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  actions: {
    async chatLoginAction(userInfo: any) {
      const result = await chatLogin(userInfo)
      if (result.flag) this.userInfo = result.data
      return result
    },
    async chatRegisterAction(userInfo: any) {
      const result = await chatRegister(userInfo)
      if (result.flag) this.userInfo.username = userInfo.username
      return result
    }
  }
})
